
function checkExecutableSkill_NightFall(obj)  
{
	if (!obj) return false;

	local isUseSkill = obj.sq_IsUseSkill(SKILL_NIGHT_FALL );
	if (isUseSkill) 
		{

			obj.getVar("GLOBAL_PARAMETER_CANCEL").set_vector(0,0);
			if (obj.getZPos() <= 0)
			{
				local pIntVec = sq_GetGlobalIntVector();
				sq_IntVectorClear(pIntVec);
				sq_IntVectorPush(pIntVec, 0);			
				obj.addSetStatePacket(STATE_NIGHT_FALL , pIntVec,  STATE_PRIORITY_USER, false, "");
			}else 
			{
				local pIntVec = sq_GetGlobalIntVector();
				sq_IntVectorClear(pIntVec);
				sq_IntVectorPush(pIntVec, 2);			
				obj.addSetStatePacket(STATE_NIGHT_FALL , pIntVec,  STATE_PRIORITY_USER, false, "");
			}

		return true;
	}
	return false;
}


function preLoad_NightFall(obj)
{
	local level = sq_GetSkillLevel(obj, SKILL_NIGHT_FALL );
	if (level > 0)
	{
		createFristAnimationPooledObject(obj,
		"passiveobject/zrr_skill/atswordman/animation/atnightfall/slashend_00.ani");
		createFristAnimationPooledObject(obj,
		"character/swordman/effect/animation/atnightfall/nightfallreadyline_00.ani");
		createFristAnimationPooledObject(obj,
		"passiveobject/zrr_skill/atswordman/animation/atnightfall/cracka_00.ani");
		createFristAnimationPooledObject(obj,
		"passiveobject/zrr_skill/atswordman/animation/atnightfall/crackb_00.ani");


	}
}


function checkCommandEnable_NightFall(obj)
{
	return true;
}

function onEndState_NightFall(obj,new_state)
{

	if (new_state != STATE_NIGHT_FALL)
	{
	
		obj.setShowEquipmentLayer(ENUM_EQUIPMENTTYPE_WEAPON, true);

	}
}


function onSetState_NightFall(obj, state, datas, isResetTimer)
{

	obj.sq_StopMove();
	local state = obj.sq_GetVectorData(datas, 0);
	obj.setSkillSubState(state);

	if (state == 0)
	{

		obj.getVar("soundRandom").clear_vector();
		obj.getVar("soundRandom").push_vector( sq_getRandom(0,100).tointeger() );
		//< 50 A
		//> 50 B

		if (obj.getVar("soundRandom").get_vector(0) < 50)
			playSoundTime(obj,"SW_NIGHTFALL_01_A",100);
		else
			playSoundTime(obj,"SW_NIGHTFALL_01_C",100);

		obj.setShowEquipmentLayer(ENUM_EQUIPMENTTYPE_WEAPON, false);
		obj.sq_SetCurrentAnimation( CUSTOM_ANI_NIGHT_FALL_READY );

		local pAni = obj.sq_GetCurrentAni();
		pAni.setSpeedRate(150.0 );
		sq_Rewind(pAni);

	}else if (state == 1)
	{
	
		if (obj.getVar("soundRandom").get_vector(0) < 50)
			playSoundTime(obj,"SW_NIGHTFALL_02_A",100);
		else
			playSoundTime(obj,"SW_NIGHTFALL_02_B",100);

		obj.sq_SetCurrentAnimation( CUSTOM_ANI_NIGHT_FALL_ATTACK );
		obj.sq_SetCurrentAttackInfo(ATTACKINFO_NIGHT_FALL);
		
		obj.resetHitObjectList();

		local speed = 0;
			
		obj.sq_SetStaticMoveInfo(0, speed, speed, false);	
		obj.sq_SetMoveDirection(obj.getDirection(), ENUM_DIRECTION_NEUTRAL);	


		obj.flashScreenOnlyMe(obj,[0,200,150],[0,0,0,102],2);
		obj.sq_setShakeOnlyMe(obj,4,300);


		local attackBonusRate = obj.sq_GetPowerWithPassive(SKILL_NIGHT_FALL,STATE_NIGHT_FALL, 0,-1,1.0);	 
		obj.sq_StartWrite();
		obj.sq_WriteDword(attackBonusRate);
		obj.sq_WriteDword(258);
		obj.sq_WriteDword(100);
		obj.sq_WriteDword(600);
		obj.sq_WriteDword(300);
		obj.sq_SendCreatePassiveObjectPacket(24357, 0,100, 1,0);
		
		createCrackNightFallHitObject(obj);
		createNightFallLineEffect(obj);

		local pAni = obj.sq_GetCurrentAni();
		pAni.setSpeedRate(200.0 );
		sq_Rewind(pAni);
	}else if (state == 2)
	{

		obj.getVar("soundRandom").clear_vector();
		obj.getVar("soundRandom").push_vector( sq_getRandom(0,100).tointeger() );
		//< 50 A
		//> 50 B

		if (obj.getVar("soundRandom").get_vector(0) < 50)
			playSoundTime(obj,"SW_NIGHTFALL_01_A",100);
		else
			playSoundTime(obj,"SW_NIGHTFALL_01_C",100);

		obj.setShowEquipmentLayer(ENUM_EQUIPMENTTYPE_WEAPON, false);
		obj.sq_SetCurrentAnimation( CUSTOM_ANI_NIGHT_FALL_AIR_READY );
		local pAni = obj.sq_GetCurrentAni();
		pAni.setSpeedRate(150.0 );
		sq_Rewind(pAni);
		
	}else if (state == 3)
	{



		obj.sq_SetCurrentAnimation( CUSTOM_ANI_NIGHT_FALL_READY );
		local pAni = obj.sq_GetCurrentAni();
		pAni.setSpeedRate(150.0 );
		sq_Rewind(pAni);
	
	}

	obj.getVar("flag").clear_vector();
	obj.getVar("flag").push_vector(0);
	obj.getVar("flag").push_vector(0);
	obj.getVar("flag").push_vector(0);
	obj.getVar("flag").push_vector(0);	
	obj.sq_SetStaticSpeedInfo(SPEED_TYPE_ATTACK_SPEED, SPEED_TYPE_ATTACK_SPEED,
	SPEED_VALUE_DEFAULT, SPEED_VALUE_DEFAULT, 1.0, 1.0);
}


function onProc_NightFall(obj)
{

	local state = obj.getSkillSubState();
	if (state == 0)
	{
		local pAni = obj.sq_GetCurrentAni();
		local frmIndex = obj.sq_GetCurrentFrameIndex(pAni);

		if (frmIndex >= 2 && obj.getVar("flag").get_vector(0) == 0)
		{
			obj.getVar("flag").set_vector(0,1);
			local speed = -400;
				
			obj.sq_SetStaticMoveInfo(0, speed, speed, false);	
			obj.sq_SetMoveDirection(obj.getDirection(), ENUM_DIRECTION_NEUTRAL);	
		}



		local currentT = sq_GetCurrentTime(pAni);
		
		if (sq_IsEnd(pAni) == ture)
		{

			local pIntVec = sq_GetGlobalIntVector();
			sq_IntVectorClear(pIntVec);
			sq_IntVectorPush(pIntVec, 1);			
			obj.addSetStatePacket(STATE_NIGHT_FALL , pIntVec,  STATE_PRIORITY_USER, false, "");

		}
	
	}else if (state == 1)
	{
	
		local pAni = obj.sq_GetCurrentAni();
		local frmIndex = obj.sq_GetCurrentFrameIndex(pAni);

		local currentT = sq_GetCurrentTime(pAni);
		
		if (sq_IsEnd(pAni) == ture)
		{

			local pIntVec = sq_GetGlobalIntVector();
			sq_IntVectorClear(pIntVec);
			sq_IntVectorPush(pIntVec, 1);			
			obj.addSetStatePacket(0 , pIntVec,  STATE_PRIORITY_USER, false, "");

		}
	
	}else if (state == 2)
	{
	
		local pAni = obj.sq_GetCurrentAni();
		local frmIndex = obj.sq_GetCurrentFrameIndex(pAni);

		local currentT = sq_GetCurrentTime(pAni);
		
		if (sq_IsEnd(pAni) == ture)
		{

			local pIntVec = sq_GetGlobalIntVector();
			sq_IntVectorClear(pIntVec);
			sq_IntVectorPush(pIntVec, 3);			
			obj.addSetStatePacket(STATE_NIGHT_FALL , pIntVec,  STATE_PRIORITY_USER, false, "");

		}


	
	}else if (state == 3)
	{
	
		local pAni = obj.sq_GetCurrentAni();
		local frmIndex = obj.sq_GetCurrentFrameIndex(pAni);

		local currentT = sq_GetCurrentTime(pAni);

		local pAni = obj.sq_GetCurrentAni();
		local frmIndex = obj.sq_GetCurrentFrameIndex(pAni);

		if (frmIndex >= 2 && obj.getVar("flag").get_vector(0) == 0)
		{
			obj.getVar("flag").set_vector(0,1);
			local speed = -400;
				
			obj.sq_SetStaticMoveInfo(0, speed, speed, false);	
			obj.sq_SetMoveDirection(obj.getDirection(), ENUM_DIRECTION_NEUTRAL);	
			sq_SetZVelocity(obj,100,-5000);
		}

		if (obj.getZPos() <= 0)
		{

			local pIntVec = sq_GetGlobalIntVector();
			sq_IntVectorClear(pIntVec);
			sq_IntVectorPush(pIntVec, 1);			
			obj.addSetStatePacket(STATE_NIGHT_FALL , pIntVec,  STATE_PRIORITY_USER, false, "");

		}
	
	}

}

function createNightFallEndAnimation(obj)
{
	if(!obj || !sq_IsMyControlObject(obj) )
	return ;
	local ani = sq_CreateAnimation("","passiveobject/zrr_skill/atswordman/animation/atnightfall/slashend_00.ani");
	local pooledObj = sq_CreatePooledObject(ani,true);
	pooledObj.setCurrentPos(obj.getXPos() ,obj.getYPos() + 1,obj.getZPos() );
	pooledObj.setCurrentDirection(obj.getDirection());
	sq_AddObject(obj,pooledObj,2,false);	
}

function createNightFallLineEffect(obj)
{

	if(!obj || !sq_IsMyControlObject(obj) )
	return ;
	local ani = sq_CreateAnimation("","character/swordman/effect/animation/atnightfall/nightfallreadyline_00.ani");
	local pooledObj = sq_CreatePooledObject(ani,true);
	pooledObj.setCurrentPos(obj.getXPos() ,obj.getYPos() + 1,obj.getZPos() );
	pooledObj.setCurrentDirection(obj.getDirection());
	sq_AddObject(obj,pooledObj,2,false);	
}

function createCrackNightFallHitObject(obj)
{

	if(!obj || !sq_IsMyControlObject(obj) )
	return ;
	if (sq_getRandom(0,100).tointeger() < 50)
	{
	
		local ani = sq_CreateAnimation("","passiveobject/zrr_skill/atswordman/animation/atnightfall/cracka_00.ani");
		local pooledObj = sq_CreatePooledObject(ani,true);
		
		pooledObj.setCurrentPos(obj.getXPos() ,obj.getYPos() - 1,obj.getZPos() );
		pooledObj.setCurrentDirection(obj.getDirection());
		sq_AddObject(obj,pooledObj,2,false);	
		sq_ChangeDrawLayer(pooledObj, ENUM_DRAWLAYER_BOTTOM );
	}else
	{

		local ani = sq_CreateAnimation("","passiveobject/zrr_skill/atswordman/animation/atnightfall/crackb_00.ani");
		local pooledObj = sq_CreatePooledObject(ani,true);
		
		pooledObj.setCurrentPos(obj.getXPos() ,obj.getYPos() - 1,obj.getZPos() );
		pooledObj.setCurrentDirection(obj.getDirection());
		sq_AddObject(obj,pooledObj,2,false);
		sq_ChangeDrawLayer(pooledObj, ENUM_DRAWLAYER_BOTTOM );

	}

}


